<?php

/**
 * @author gbfun <gbfun@163.com>  2018-06-10 16:03
 * http://www.wuweiyou.com
 */

class db_model_member
{
    protected $_db_table;   
    protected $_formatter;
    
    protected static $_instance;
    
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    private function __clone()
    {
    }
    
    protected function __construct()
    {
    }

    public function get_db_table()
    {
        if (null === $this->_db_table) {
            $this->_db_table = db_table_member::get_instance();
        }
        
        return $this->_db_table;
    }
    
    public function get_formatter()
    {
        if (null === $this->_formatter) {
            $this->_formatter = formatter_member::get_instance();
        }
    
        return $this->_formatter;
    }
    
    public function set_rows(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row($row);            
        }
    }
    
    public function set_row(& $row)
    {
        $formatter = $this->get_formatter();
        
        //if(!empty($row['last_login_time'])){
        if(isset($row['last_login_time'])){
            $formatter->set_by_last_login_time($row);
        }
        
        if(!empty($row['create_time'])){
            $formatter->set_by_create_time($row);
        }
        if(!empty($row['update_time'])){
            $formatter->set_by_update_time($row);
        }
        if(isset($row['status'])){
            $formatter->set_by_status($row);
        }
    }
    
    public function set_rows_user(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row_user($row);
        }
    }
    
    public function set_row_user(& $row)
    {

    }
    
    public function find($id)
    {
        $row = $this->get_db_table()->find($id);
        //var_dump($row); exit();
    
        if(!empty($row)){
            $this->set_row($row);
            //var_dump($row); exit();
        }
    
        return $row;
    }
    
    public function fetch_row($where)
    {
        $row = $this->get_db_table()->fetch_row($where);
        //var_dump($row); exit();
    
        if(!empty($row)){
            $this->set_row($row);
            //var_dump($row); exit();
        }
    
        return $row;
    }
    
    public function fetch_all($where = null, $order = null)
    {
        $rows = $this->get_db_table()->fetch_all($where, $order);
        //var_dump($rows); exit();
    
        $this->set_rows($rows);
        //var_dump($rows); exit();
    
        return $rows;
    }
    
    public function get_list($where = null, $order = null, $options = array())
    {
        $list = $this->get_db_table()->get_list($where, $order, $options);
        //var_dump($list); exit();
    
//         $rows = $list['list'];
//         $this->set_rows($rows);
//         $list['list'] = $rows;
        $this->set_rows($list['list']);
        //var_dump($list); exit();
    
        return $list;
    }
    
    public function get_simple_list($where = null, $order = null, $options = array())
    {
    
    }
    
    public function mobile_add($member_info)
    {
        $this->set_row_user($member_info);
        //var_dump($good_info); exit();
    
        $add_result = $this->get_db_table()->mobile_add($member_info);
        //var_dump($add_result);
    
        return $add_result;
    }
    
    public function update_member_status($member_info)
    {    
        $update_member_status_result = $this->get_db_table()->update_member_status($member_info);
        //var_dump($update_member_status_result);
    
        return $update_member_status_result;
    }
    
    public function update_member_login_count($member_info)
    {
        $update_member_login_count_result = $this->get_db_table()->update_member_login_count($member_info);
        //var_dump($update_member_login_count_result);
    
        return $update_member_login_count_result;
    }
    
    public function update_member_password($member_info)
    {
        $update_member_password_result = $this->get_db_table()->update_member_password($member_info);
        //var_dump($update_member_password_result);
    
        return $update_member_password_result;
    }
    
    public function update_default_address($member_info)
    {
        $update_default_address_result = $this->get_db_table()->update_default_address($member_info);
        //var_dump($update_default_address_result);
    
        return $update_default_address_result;
    }
}

